import { PLAY_MODE } from '@/assets/js/constant'
import { shuffle } from '@/assets/js/util' // 洗牌算法

export function selectPlay({ commit, state }, { list, index }) {
  commit('setPlayMode', PLAY_MODE.sequence)
  commit('setSequenceList', list)
  commit('setPlayingState', true)
  commit('setFullScreen', true)
  commit('setPlaylist', list)
  commit('setCurrentIndex', index)
}

export function randomPlay({ commit, state }, list) {
  commit('setPlayMode', PLAY_MODE.random)
  commit('setSequenceList', list)
  commit('setPlayingState', true)
  commit('setFullScreen', true)
  commit('setPlaylist', shuffle(list))
  commit('setCurrentIndex', 0)
}
// 更改播放模式
export function changeMode({ commit, state, getters }, mode) {
  const currentId = getters.currentSong.id
  if (mode === PLAY_MODE.random) {
    commit('setPlaylist', shuffle(state.sequenceList))
  } else {
    commit('setPlaylist', state.sequenceList)
  }
  // 避免在更新列表时候切换歌曲(保证还在当前的歌曲)
  console.log(state)
  const index = state.playlist.findIndex((song) => {
    return song.id === currentId
  })
  commit('setCurrentIndex', index)
  commit('setPlayMode', mode)
}
// 移除列表歌曲
export function removeSong({ commit, state }, song) {
  const sequenceList = state.sequenceList.slice()
  const playlist = state.playlist.slice()

  const sequenceIndex = findIndex(sequenceList, song)
  const removeIndex = findIndex(playlist, song)
  if (sequenceIndex < 0 || removeIndex < 0) {
    return false
  }
  // 从当前列表中移除一首歌曲
  sequenceList.splice(sequenceIndex, 1)
  playlist.splice(removeIndex, 1)
  let currentIndex = state.currentIndex
  // 为了避免移除歌曲时候播放的歌曲发生切换，以及是最后一首歌曲清0处理
  if (removeIndex < currentIndex || currentIndex === playlist.length) {
    currentIndex--
  }
  commit('setSequenceList', sequenceList)
  commit('setPlaylist', playlist)
  commit('setCurrentIndex', currentIndex)

  if (!playlist.length) {
    commit('setPlayingState', false)
  }
}
// 清空列表
export function clearSongList({ commit }) {
  commit('setSequenceList', [])
  commit('setPlaylist', [])
  commit('setCurrentIndex', 0)
  commit('setPlayingState', false)
}
// 添加歌曲(来自search)
export function addSong({ commit, state }, song) {
  const playlist = state.playlist.slice() // 播放列表
  const sequenceList = state.sequenceList.slice() // 循环列表
  let currentIndex = state.currentIndex // 当前歌曲索引
  const playIndex = findIndex(playlist, song)
  if (playIndex > -1) {
    currentIndex = playIndex
  } else {
    playlist.push(song)
    currentIndex = playlist.length - 1
  }
  const sequenceIndex = findIndex(sequenceList, song)
  if (sequenceIndex === -1) {
    sequenceList.push(song)
  }
  commit('setSequenceList', sequenceList)
  commit('setPlaylist', playlist)
  commit('setCurrentIndex', currentIndex)
  commit('setPlayingState', true)
  commit('setFullScreen', true)
}
// 找出对应的索引值
function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}
